﻿///////////////////////////////////////////////////////////////////////////////
//
// This file is part of the BassDrop project
//   https://code.google.com/p/bass-drop/
//
//
// Creator:	Gergely Krajcsovszki (krajcsovszkig@hotmail.com)
// Date:	2014-04-09 22:25
//
//
// Copyright © 2013-2014. Gergely Krajcsovszki
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BassDrop.Core.Common.Localization;
using BassDrop.Core.Common.Options.Items;
using BassDrop.Core.Common.Services;
using BassDrop.Core.Common.Utilities;

namespace BassDrop.Core.Common.Options.OptionsSets
{
	/// <summary>
	/// Class containing the general options items
	/// </summary>
	public class GeneralOptionsSet : OptionsSetBase
	{
		/// <summary>
		/// The name of this options set
		/// </summary>
		public override string Name
		{
			get
			{
				return Strings.LabelSettingsViewTreeViewItemGeneral;
			}
		}

		private EnumPropertyOptionsItem m_languageOptionsItem;

		/// <summary>
		/// Constructs a new general options set
		/// </summary>
		public GeneralOptionsSet()
		{
			m_languageOptionsItem = new EnumPropertyOptionsItem(
					SetLanguage,
					GetLanguage,
					Language.AvailableLanguages.ToArray(),
					GetLanguageName,
					EnumPropertyOptionsItemDisplayType.ComboBox,
					Strings.LabelOptionsSetGeneralLanguage,
					null
				);

			OptionList.Add( m_languageOptionsItem );

			Strings.StaticPropertyChanged += Strings_StaticPropertyChanged;
			Controller.Instance.ApplicationLanguageChanged += Instance_ApplicationLanguageChanged;
		}

		private object GetLanguage()
		{
			return Controller.Instance.ApplicationLanguage;
		}

		private void SetLanguage( object value )
		{
			Controller.Instance.ApplicationLanguage = (Language) value;
		}

		private string GetLanguageName( object value )
		{
			return ((Language) value).Name;
		}

		/// <summary>
		/// Loads the options
		/// </summary>
		/// <param name="settings">the seettings object to load from</param>
		public override Task Load( ISettings settings )
		{
			if ( !String.IsNullOrEmpty( settings.SettingAt( "Language" ) ) )
			{
				SetLanguage( Language.ForLanguage( settings.SettingAt( "Language" ) ) );
			}

			return ThreadingUtils.CompletedTask();
		}

		/// <summary>
		/// Saves the options
		/// </summary>
		/// <param name="settings">the seettings object to save to</param>
		public override void Save( ISettings settings )
		{
			settings.SetSettingAt( "Language", ((Language) GetLanguage()).LanguageCode );
		}

		private void Strings_StaticPropertyChanged( object sender, PropertyChangedEventArgs e )
		{
			if ( e.PropertyName == "LabelSettingsViewTreeViewItemGeneral" )
			{
				OnPropertyChanged( new PropertyChangedEventArgs( "Name" ) );
			}
			else if ( e.PropertyName == "LabelOptionsSetGeneralLanguage" )
			{
				m_languageOptionsItem.Name = Strings.LabelOptionsSetGeneralLanguage;
			}
		}

		private void Instance_ApplicationLanguageChanged( object sender, PropertyChangedEventArgs e )
		{
			OnPropertyChanged( new PropertyChangedEventArgs( "Language" ) );
		}
	}
}
